<?php
class CaseController extends AdminAppController
{
    public $uses = array('Admin.Work', 'Admin.Tag', 'Admin.CaseTag');

    public $paginate = array(
        'fields' => array('Work.id', 'Work.title', 'Work.priority', 'Work.service_id', 'Work.created', 'Work.updated'),
        'limit' => 25,
        'order' => array(
            'Work.priority' => 'desc',
            'Work.updated' => 'desc',
        ),
        'conditions' => array(
            'Work.is_deleted' => 0
        )
    );

    public function index()
    {
        $data = $this->paginate('Work');
        $services = $this->Work->Service->find('list');

        $this->set(compact('services', 'data'));
    }

    public function add()
    {
        if ($this->request->is('post')) {
            try {
                $caseId = $this->Work->saveCase($this->request->data);
                if ($caseId) {
                    $tags = $this->request->data['Tag']['tag_id'];
                    if ($tags) {
                        $caseTag = array();
                        foreach($tags as $tag) {
                            $caseTag[] = array(
                                'CaseTag' => array(
                                    'case_id' => $caseId,
                                    'tag_id' => $tag
                                )
                            );
                        }
                        $res = $this->CaseTag->saveAll($caseTag); 
                    }
                }
                
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The case has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Work->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
        $services = $this->Work->Service->find('list');
        $tagList = $this->Tag->find('list');
        $this->set(compact('services', 'tagList'));
    }

    public function edit($id=null)
    {
        $case = $this->Work->getCase($id);
        $caseTag = $this->CaseTag->find('all', array(
            'conditions' => array(
                'case_id' => $id
            )
        ));
        if ($this->request->is('get')) {
            $this->request->data = $case;
            $this->request->data['Tag'] = array('tag_id'=>array());
            foreach ($caseTag as $relation) {
                $this->request->data['Tag']['tag_id'][] = $relation['CaseTag']['tag_id'];
            }
        } else {
            try {
                $isUpdate = true;
                $this->Work->saveCase($this->request->data, $isUpdate, $case);

                $this->CaseTag->deleteAll(array('case_id'=>$id));

                $tags = $this->request->data['Tag']['tag_id'];
                if ($tags) {
                    $caseTag = array();
                    foreach($tags as $tag) {
                        $caseTag[] = array(
                            'CaseTag' => array(
                                'case_id' => $id,
                                'tag_id' => $tag
                            )
                        );
                    }
                    $res = $this->CaseTag->saveAll($caseTag); 
                }

                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The log has been saved'));
            } catch (Exception $e) {
                $this->request->data = $this->_mergeData($case);
                $validationErrors = $this->Work->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
        $services = $this->Work->Service->find('list');
        $tagList = $this->Tag->find('list');
        $this->set(compact('services', 'tagList'));
    }

    public function delete($id=null)
    {
        if ($id == null) {
            $this->redirect('/');
        }
        $data = array(
            'Work' => array(
                'id' => $id,
                'is_deleted' => 1
            )
        );
        $this->Work->save($data);
        $this->redirect('/admin/case');
    }

    public function _mergeData($case)
    {
        $request = $this->request->data['Admin']['Case'];
        return $this->request->data; 
    }
}
